package com.ruoyi.ly.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.ly.domain.SpaceFloor;
import com.ruoyi.ly.domain.dto.PageSpaceFloorDTO;
import com.ruoyi.ly.domain.dto.SpaceFloorAddDTO;
import com.ruoyi.ly.domain.dto.SpaceFloorUpdateDTO;
import com.ruoyi.ly.domain.vo.PageSpaceFloorVO;
import com.ruoyi.ly.domain.vo.SpaceBanUpdateVO;
import com.ruoyi.ly.domain.vo.SpaceFloorVO;
import com.ruoyi.ly.mapper.SpaceBanMapper;
import com.ruoyi.ly.mapper.SpaceFloorMapper;
import com.ruoyi.ly.service.SpaceFloorService;
import com.ruoyi.ly.utils.ExportData;
import com.ruoyi.ly.utils.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

@Service
@Slf4j
public class SpaceFloorServiceImpl implements SpaceFloorService {

    @Autowired
    private SpaceFloorMapper spaceFloorMapper;
    @Autowired
    private ExportData exportData;
    @Autowired
    private SpaceBanMapper spaceBanMapper;

    @Override
    //修改楼层
    public void update(SpaceFloorUpdateDTO spaceFloorUpdateDto) {


        //获取该楼层所属楼栋的总层数
        SpaceBanUpdateVO spaceBanUpdateVO = spaceBanMapper.selectById(spaceFloorUpdateDto.getBanId());
        Long banNunbers = spaceBanUpdateVO.getBanNumbers();

        //判断增加楼层是否合法
        if(spaceFloorUpdateDto.getFloorNumber() <= 0 )
        {
            throw new ServiceException("楼层输入不合法");
        }
        else if(spaceFloorUpdateDto.getFloorNumber() > banNunbers)
        {
            throw new ServiceException("楼层输入超过楼栋层数限制");
        }
        //判断楼栋对应的楼层是否存在  (同一楼栋不能存在相同楼层)
        List<Long> floorNumbers = spaceFloorMapper.selectAllFloorByBanId(spaceFloorUpdateDto.getBanId());
        if(floorNumbers.contains(spaceFloorUpdateDto.getFloorNumber())){
            if(spaceFloorMapper.selectFloorNumberByFloorId(spaceFloorUpdateDto.getFloorId()) != spaceFloorUpdateDto.getFloorNumber())
            {
                //修改楼层时 楼层号没有改变可以修改
                throw new ServiceException("该楼栋已存在该楼层");
            }

        }

        SpaceFloor spaceFloor =new SpaceFloor();
        BeanUtils.copyProperties(spaceFloorUpdateDto,spaceFloor);
        spaceFloorMapper.update(spaceFloor);
    }

    @Override
    //  新增楼层信息
    public void save(SpaceFloorAddDTO spaceFloorAddDTO) {

        //获取该楼层所属楼栋的总层数
        SpaceBanUpdateVO spaceBanUpdateVO = spaceBanMapper.selectById(spaceFloorAddDTO.getBanId());
        Long banNunbers = spaceBanUpdateVO.getBanNumbers();

        //判断增加楼层是否合法
        if(spaceFloorAddDTO.getFloorNumber() <= 0 )
        {
            throw new ServiceException("楼层输入不合法");
        }
        else if(spaceFloorAddDTO.getFloorNumber() > banNunbers)
        {
            throw new ServiceException("楼层输入超过楼栋层数限制");
        }

        //判断楼栋对应的楼层是否存在  (同一楼栋不能存在相同楼层)
        List<Long> floorNumbers = spaceFloorMapper.selectAllFloorByBanId(spaceFloorAddDTO.getBanId());
        if(floorNumbers.contains(spaceFloorAddDTO.getFloorNumber())){
            throw new ServiceException("该楼栋已存在该楼层");
        }

        SpaceFloor spaceFloor =new SpaceFloor();
        BeanUtils.copyProperties(spaceFloorAddDTO,spaceFloor);
        spaceFloor.setCtime(String.valueOf(LocalDate.now()));
        spaceFloorMapper.insert(spaceFloor);
    }

    @Override
    //分页查询楼层信息
    public PageResult pageQuery(PageSpaceFloorDTO pageSpaceFloorDTO) {
        PageHelper.startPage(pageSpaceFloorDTO.getPage(),pageSpaceFloorDTO.getPageSize());
        Page<PageSpaceFloorVO> page = spaceFloorMapper.pageQuery(pageSpaceFloorDTO);
        long total = page.getTotal();
        List<PageSpaceFloorVO> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    //删除楼层信息
    public void delete(Long floorId) {
        spaceFloorMapper.delete(floorId);
    }

    @Override
    //根据楼层id查询楼层信息
    public SpaceFloorVO selectById(Long floorId) {
        SpaceFloorVO spaceFloorVO = spaceFloorMapper.selectById(floorId);
        return spaceFloorVO;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        //查询所有楼层的集合
        List<PageSpaceFloorVO> spaceFloors = spaceFloorMapper.selectAll();
        exportData.exportDataEasyExcel(response,spaceFloors,PageSpaceFloorVO.class,"楼层信息");
    }
}
